<?php              
define('DEFAULT_TABLE_ROWS', 1);

$var_cui_furnizor = 0;
$var_id_program = 0;
$var_id_sesiune = 0;

if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['cui_furnizor']) and ( isset($_POST['add_furnizor']) or isset($_POST['edit_furnizor']) or 
isset($_GET['view_furnizor']) or isset($_POST['view_furnizor'])) )
{
	$var_cui_furnizor = $_POST['cui_furnizor'];
}
else  if($_SERVER['REQUEST_METHOD'] == "GET" and isset($_GET['cui_furnizor']) and ( isset($_GET['add_furnizor']) or isset($_GET['edit_furnizor']) or 
isset($_GET['view_furnizor']) or isset($_POST['view_furnizor']) ) )
{
	$var_cui_furnizor = $_GET['cui_furnizor'];
}

include "javascript".DIRECTORY_SEPARATOR."serverside.php";
include "includes".DIRECTORY_SEPARATOR."include_user_manufacturer_functions.php";
include "includes".DIRECTORY_SEPARATOR."include_user_manufacturer_forms_for_tables.php";
include "functions".DIRECTORY_SEPARATOR."utils.php";
include "javascript".DIRECTORY_SEPARATOR."js_functions.php";
			
				
$form_fields_with_speficications = array();

for($index = 1; $index <= DEFAULT_TABLE_ROWS; $index+=1) 
{
	foreach( $form_for_table as $form_column_table )
	{
		array_push( $form_fields_with_speficications,$form_column_table);
	}
}

	
foreach($form_fields_with_speficications as $form_key => $form_value)
{
	if( ! isset($form_fields_with_speficications[$form_key]['colors']) )
			$form_fields_with_speficications[$form_key]['colors'] = array('first' => '#333333', 'second' => 'red');

	if( ! isset($form_fields_with_speficications[$form_key]['field_size']) )
			$form_fields_with_speficications[$form_key]['field_size'] = null;
	if( ! isset($form_fields_with_speficications[$form_key]['max_length']) )
			$form_fields_with_speficications[$form_key]['max_length'] = null;
	if( ! isset($form_fields_with_speficications[$form_key]['regex']) )
			$form_fields_with_speficications[$form_key]['colors'] = null;						  
	if( ! isset($form_fields_with_speficications[$form_key]['can_be_null']) )
			$form_fields_with_speficications[$form_key]['can_be_null'] =  false;
	
	if( ! isset($form_fields_with_speficications[$form_key]['current_value']))
	{
		if( $form_fields_with_speficications[$form_key]['field_type'] == 'text')
		{
			if( isset($form_fields_with_speficications[$form_key]['initial_values']) )
				$form_fields_with_speficications[$form_key]['current_value'] = $form_fields_with_speficications[$form_key]['initial_values'];
			else
				$form_fields_with_speficications[$form_key]['current_value'] =  '';
		}
		else if( $form_fields_with_speficications[$form_key]['field_type'] == 'drop_down_list')
		{
			$form_fields_with_speficications[$form_key]['current_value'] = "neselectat";
		}
	}
	
	$form_fields_with_speficications[$form_key]['current_color'] = $form_fields_with_speficications[$form_key]['colors']['first'];	
}

$form_content = generate_form_content($form_fields_with_speficications);

$erori = "";


if ( isset($_GET['add_furnizor']) or isset($_POST['add_furnizor']) )
{
$get_form=<<<GATA
	<form method="post" action = "user_commission_search.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="add_furnizor" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<tr><td></td>
				<td><input type ="submit" value = "Salveaza" ></td>
			</tr>
		</table>
	</form>
GATA;
}
else if ( isset($_GET['edit_furnizor']) or isset($_POST['edit_furnizor']) )
{
$get_form=<<<GATA
	<form method="post" action = "user_commission_search.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="edit_furnizor" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<tr><td></td>
				<td><input type ="submit" value = "Salveaza" ></td>
			</tr>
		</table>
	</form>
GATA;
} 
else if ( isset($_GET['view_furnizor']) or isset($_POST['view_furnizor']) )
{
$get_form=<<<GATA
	<form method="post" action = "user_commission_search.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="'view_furnizor" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
		</table>
	</form>
GATA;
} 
else // ADD FURNIZO TAB
{
$get_form=<<<GATA
	<form method="post" action = "user_commission_manufacturers.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<tr><td></td>
				<td><input type ="submit" value = "Salveaza" ></td>
			</tr>
		</table>
	</form>
GATA;
}
	
function has_value($input_name){
	if ( isset($_POST[$input_name]) && !empty($_POST[$input_name]))
		return true;
	else 
		return false;
}

function get_value($input_name){
	if ( isset($_POST[$input_name]) && !empty($_POST[$input_name]))
		return $_POST[$input_name];
	else 
		return NULL;
}

# search into table "furnizor" by CUI/CF
function search_by_cui($search_value, $dbc){
	echo $search_value;
	require_once ('mysql_connect.php');
	$rezs = mysqli_query($dbc, "SELECT `nume`,`CUI_CF`,`numar_decizie_autorizare`,`localitate`,`telefon`,`email` FROM anc.furnizor WHERE `CUI_CF` like '%$search_value%'");
	while ($rows = mysqli_fetch_assoc($rezs)){ 
		
		?>
        <script type='text/javascript'>	
		
		 addToTable('<?php echo $rows['nume']; ?>', 
		   '<?php echo $rows['CUI_CF']; ?>',  
		   '<?php echo $rows['numar_decizie_autorizare']; ?>',
		   '<?php echo $rows['localitate']; ?>',
		   '<?php echo $rows['telefon']; ?>',
		   '<?php echo $rows['email']; ?>' ); 
		</script>
		<?php
	}
	?>
   
    <?php
	
}


if($_SERVER['REQUEST_METHOD'] == 'GET')
{
	echo $get_form;
}


if($_SERVER['REQUEST_METHOD'] == 'POST')
{

	if ( has_value("searched_cui_cf") || has_value("provider_name")){
	
		echo "SEARCH";
		$sccv = get_value("searched_cui_cf") ;
		
		if ( has_value("searched_cui_cf") && $sccv!=NULL ){
			
			search_by_cui($sccv,$dbc);
		}
			
		echo $get_form;
	}
	else{	
		$ok = 1;
		$nr_cui_cf = 0;
			foreach($form_fields_with_speficications as $form_key => $form_value)
			{
				if(isset($form_fields_with_speficications[$form_key]['post_name']) && isset($_POST[$form_fields_with_speficications[$form_key]['post_name']]))
				{									
		         //check that at least one of CF / CUI is completed
				 /*
				 if ($form_fields_with_speficications[$form_key]['post_name'] == 'CUI' 
				 	|| $form_fields_with_speficications[$form_key]['post_name'] == 'CF')
				 	if (!empty($_POST[$form_fields_with_speficications[$form_key]['post_name']]))
						$nr_cui_cf =  $nr_cui_cf + 1;
				*/
		
				if(empty($_POST[$form_fields_with_speficications[$form_key]['post_name']]) 
				|| ( $_POST[$form_fields_with_speficications[$form_key]['post_name']] == 'neselectat') )
				//if (( $_POST[$form_fields_with_speficications[$form_key]['post_name']] == 'neselectat'))
				{
					# Something went wrong with this form element so an error will be displayed to user
					$ok = 0;
					
					# Change color for element ; default will be red
					$form_fields_with_speficications[$form_key]['current_color'] = "red";
					if( isset($form_fields_with_speficications[$form_key]['colors'] ))
					{
						if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
						{
							$form_fields_with_speficications[$form_key]['current_color'] = 
							$form_fields_with_speficications[$form_key]['colors']['second'];		
						}
					}
					
					# Generate an error if nothing it's provided
					$default_generated_error = "";
					
					if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
					{
						$default_generated_error = 
						"Nu a fost introdus nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
					}
					else if( $form_fields_with_speficications[$form_key]['field_type'] == "drop_down_list" )
					{
						$default_generated_error =
						 "Nu a fost selectat nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
					}
					else
					{
						$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['post_name']." este gol</br>";
					}
					
					# Add the provided error or the default one
					if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
					{
						if( isset($form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'] ))
						{
							$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'];					
						}
						else
						{
							$erori = $erori.$default_generated_error;
						}
					}
					else
					{
						$erori = $erori.$default_generated_error;
					}
					
					continue;
				}
				
				# check the values of CUI /CF; 
				# Set the error message
				//$form_fields_with_speficications = checkCUICF($cui_cf_completed,$form_fields_with_speficications);
				
				# Check regex here
				if ( isset( $form_fields_with_speficications[$form_key]['regex'] ) )
				{
					if ( ! empty( $form_fields_with_speficications[$form_key]['regex'] ) )
					{
						if( preg_match($form_fields_with_speficications[$form_key]['regex'],
						$_POST[$form_fields_with_speficications[$form_key]['post_name']])== 0 )
						{																					
							# Something went wrong with this form element so an error will be displayed to user
							$ok = 0;
							
							# Change color for element ; default will be red
							$form_fields_with_speficications[$form_key]['current_color'] = "red";
							if( isset($form_fields_with_speficications[$form_key]['colors'] ))
							{
								if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
								{
									$form_fields_with_speficications[$form_key]['current_color'] = 
									$form_fields_with_speficications[$form_key]['colors']['second'];		
								}
							}
							
							# Generate an error if nothing it's provided
							$default_generated_error = "";
							
							if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
							{
								$default_generated_error = "Datele introduse pentru campul ".$form_fields_with_speficications[$form_key]['field_display_name']." sunt invalide</br>";
							}
							else
							{
								$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['field_display_name']." este invalid</br>";
							}
							
							# Add the provided error or the default one
							if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
							{
								if( isset($form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'] ))
								{
									$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'];					
								}
								else
								{
									$erori = $erori.$default_generated_error;
								}
							}
							else
							{
								$erori = $erori.$default_generated_error;
							}
						
							continue;
						}
					}
				}
				
				
				# check validation functions
				if ( isset( $form_fields_with_speficications[$form_key]['validation_function'] ) )
				{
					if ( !empty( $form_fields_with_speficications[$form_key]['validation_function'] ) )
					{
						$function_name = $form_fields_with_speficications[$form_key]['validation_function'] ;
						$cif_value = $_POST[$form_fields_with_speficications[$form_key]['post_name']]; 
						
						if($function_name($cif_value) == 0 )
						{																					
							# Something went wrong with this form element so an error will be displayed to user
							$ok = 0;
							
							# Change color for element ; default will be red
							$form_fields_with_speficications[$form_key]['current_color'] = "red";
							if( isset($form_fields_with_speficications[$form_key]['colors'] ))
							{
								if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
								{
									$form_fields_with_speficications[$form_key]['current_color'] = 
									$form_fields_with_speficications[$form_key]['colors']['second'];		
								}
							}
							
							# Generate an error if nothing it's provided
							$default_generated_error = "";
							
							if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
							{
								$default_generated_error = 
								"Datele introduse pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."
								 sunt invalide</br>";
							}
							else
							{
								$default_generated_error = 
								"Campul ".$form_fields_with_speficications[$form_key]['field_display_name']." este invalid</br>";
							}
							
							# Add the provided error or the default one
							if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
							{
								if( isset($form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'] ))
								{
									$erori = 
										$erori.$form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'];					
								}
								else
								{
									$erori = $erori.$default_generated_error;
								}
							}
							else
							{
								$erori = $erori.$default_generated_error;
							}
						
							continue;
						}
					}
				}
				$form_fields_with_speficications[$form_key]['current_value'] = $_POST[$form_fields_with_speficications[$form_key]['post_name']];  											
			}
		}

if ( isset($_GET['add_furnizor']) or isset($_POST['add_furnizor']) )
{
	$form_content = generate_form_content($form_fields_with_speficications);
	$form_post=<<<GATA
		<form method="post" action = "user_commission_search.php">
			<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="add_furnizor" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<tr>
				<td></td><td><input type ="submit" value = "Salveaza" onclick="this.form.target='_self';return true;" name="comanda"></td>
			</tr>
	</table>
	</form>
GATA;
}
else if ( isset($_GET['edit_furnizor']) or isset($_POST['edit_furnizor']) )
{
	$form_content = generate_form_content($form_fields_with_speficications);
	$form_post=<<<GATA
		<form method="post" action = "user_commission_search.php">
			<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="edit_furnizor" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<tr>
				<td></td><td><input type ="submit" value = "Salveaza" onclick="this.form.target='_self';return true;" name="comanda"></td>
			</tr>
	</table>
	</form>
GATA;
}
else // ADD FURNIZOR TAB
{			
$form_content = generate_form_content($form_fields_with_speficications);
$form_post=<<<GATA
	<form method="post" action = "user_commission_manufacturers.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
		$form_content
		<tr>
			</td>
			<td><input type ="submit" value = "Adauga" 
			onclick="this.form.target='_self';return true;" name="comanda">
			</td>
		</tr>
</table>
</form>
GATA;
}
		$mail_address = "-";
		if ( defined('MAIL') )
		{
			$mail_address = MAIL;
		}
		
		if($ok != 1)
		{
			echo $form_post;
			echo "<font color=\"red\">".$erori."</font>";
		}
		else  
		{
			if ( isset($_GET['add_furnizor']) or isset($_POST['add_furnizor']) )
			{
				require_once ('mysql_connect.php');
				$cod_judet = getCountyId($_POST["judet"]);
				$region = getRegion($_POST["judet"]);				
				$u = mysqli_query($dbc,"INSERT INTO `furnizor`
				(`nume`,`CUI_CF`,`numar_decizie_autorizare`,`data_decizie_autorizare`,`forma_organizare`, 				 	   									 		`forma_proprietate`,`smac`,`regiune`,`cod_judet`,`judet`,`localizare`,`localitate`,`strada`,`nr_strada`,`bloc`,
				`scara`,`etaj`,`apartament`,`telefon`,`fax`,`email`,`website`,`email_user_comisie`)  VALUES ('$_POST[nume_furnizor]', '$_POST[CUI_CF]',  											 		'$_POST[numar_decizie_autorizare]','$_POST[data_decizie_autorizare]',   						  				   	 			 		'$_POST[forma_organizare]','$_POST[forma_proprietate]','$_POST[smac]', '$region',$cod_judet ,    																							 		'$_POST[judet]','$_POST[localizare]', '$_POST[localitate]','$_POST[strada]', '$_POST[nr_strada]', '$_POST[bloc]',  			 		'$_POST[scara]', '$_POST[etaj]','$_POST[apartament]', '$_POST[telefon]', '$_POST[fax]', '$_POST[email]', '$_POST[site]', '$mail_address')");
				
				echo "<br />";	
				echo "<center><h2><font color=\"#00CC00\">Furnizorul a fost adaugat cu succes ! </font></h2></center>";	
				echo "<br />";	
				echo $get_form;
			}
			else if ( isset($_GET['edit_furnizor']) or isset($_POST['edit_furnizor']) )
			{
				/* get the region by the selected county*/
				$cod_judet = getCountyId($_POST["judet"]);
				$region = getRegion($_POST["judet"]);
				$update_request = mysqli_query($dbc,"UPDATE `furnizor` SET `CUI_CF`='$_POST[CUI_CF]',`numar_decizie_autorizare`='$_POST[numar_decizie_autorizare]',`data_decizie_autorizare`='$_POST[data_decizie_autorizare]',`nume`='$_POST[nume_furnizor]',`regiune`='$region',`cod_judet`='$cod_judet',`judet`='$_POST[judet]',`localitate`='$_POST[localitate]',`localizare`='$_POST[localizare]',`strada`='$_POST[strada]',`nr_strada`='$_POST[nr_strada]',`bloc`='$_POST[bloc]',`scara`='$_POST[scara]',`etaj`='$_POST[etaj]',`apartament`='$_POST[apartament]',`telefon`='$_POST[telefon]',`fax`='$_POST[fax]',`email`='$_POST[email]',website='$_POST[site]',`forma_organizare`='$_POST[forma_organizare]',`forma_proprietate`='$_POST[forma_proprietate]',`smac`='$_POST[smac]', `state_anc`='neaprobat_anc' WHERE `CUI_CF`='$var_cui_furnizor'");
				
			
				if($update_request)
				{
					echo "<br />";	
					echo "<center><h2><font color=\"#00CC00\">Datele au fost modificate cu succes ! </font></h2></center>";	
					echo "<br />";
				}
				else
				{
					echo "<br />";	
					echo "<center><h2><font color=\"#FF0000\">Datele nu au fost modificate ! Reincercati </font></h2></center>";	
					echo "<br />";
					echo $get_form;
				}
			}
			else // FURNIZOR TAB
			{
							
				require_once ('mysql_connect.php');
				
				$cod_judet = getCountyId($_POST["judet"]);
				$region = getRegion($_POST["judet"]);
				
				$u = mysqli_query($dbc,"INSERT INTO `furnizor`
				(`nume`,`CUI_CF`,`numar_decizie_autorizare`,`data_decizie_autorizare`,`forma_organizare`, 				 	   									 		`forma_proprietate`,`smac`,`regiune`,`cod_judet`,`judet`,`localizare`,`localitate`,`strada`,`nr_strada`,`bloc`,
				`scara`,`etaj`,`apartament`,`telefon`,`fax`,`email`,`website`,`email_user_comisie`)  VALUES ('$_POST[nume_furnizor]', '$_POST[CUI_CF]',  											 		'$_POST[numar_decizie_autorizare]','$_POST[data_decizie_autorizare]',   						  				   	 			 		'$_POST[forma_organizare]','$_POST[forma_proprietate]','$_POST[smac]', '$region', '$cod_judet',   																							 		'$_POST[judet]','$_POST[localizare]', '$_POST[localitate]','$_POST[strada]', '$_POST[nr_strada]', '$_POST[bloc]',  			 		'$_POST[scara]', '$_POST[etaj]','$_POST[apartament]', '$_POST[telefon]', '$_POST[fax]', '$_POST[email]', '$_POST[site]', '$mail_address')");
				
				echo "<br />";	
				echo "<center><h2><font color=\"#00CC00\">Furnizorul a fost adaugat cu succes ! </font></h2></center>";	
				echo "<br />";	
				echo $get_form;
			}
		}
	}
}
				
?> 